<?php

namespace Api\Model;

use Think\Model;

class UserModel extends Model {

    /**
     * @name 根据手机号获取用户详情
     * @param string $mobile
     */
    public function getInfoByMobile($mobile) {
        $result = $this->where("`status`=1 and `mobile`='{$mobile}' ")->field('id,mobile,nickname,realname')->find();
        return $result;
    }

    /**
     * @name 用户注册
     * @param $mobile
     * @param $password
     * @return mixed
     */
    public function register($mobile, $password, $nickname) {

        $data = array (
            'mobile' => $mobile,
            'password' => $this->makePassword($password),
            'nickname' => $nickname,
            'create_time' => time()
        );

        return $this->add($data);
    }

    /**
     * @name 用户登录
     * @param $mobile
     * @param $password
     * @return $return
     */
    public function login($mobile, $password) {
        //1.验证账号是否存在
        $result = $this->where("`status`=1 and `mobile`='{$mobile}'")->field('password')->find();
        if ($result) {
            //2.验证密码是否正确
            $password = $this->makePassword($password);
            if ($result['password'] == $password) {
                $return = C('LOGIN_STATUS.SUCCESS');
            } else {
                $return = C('LOGIN_STATUS.PASSWORD_ERROR');
            }
        } else {
            $return = C('LOGIN_STATUS.INEXISTENCE');
        }
        //3.返回结果
        return $return;
    }

    /**
     * @name 生成密码
     * @param $password
     */
    public function makePassword($password) {
        return md5($password);
    }

    /**
     * @name 修改密码
     * @param $mobile
     * @param $password
     */
    public function rePassword($mobile, $password) {
        $data['password'] = $this->makePassword($password);
        return $this->where(" `mobile`='{$mobile}' ")->save($data);
    }

    /**
     * @name 生成token
     * @param $mobile
     * @return String
     */
    public function makeToken($userid, $mobile) {
        $aes = new \Common\Utils\aes\Aes();
        $deadline = time() + 6000;

        $token = $aes->passport_encrypt($userid.'-'.$mobile.'-'.$deadline, C('TOKEN_KEY'));

        return $token;
    }

    /**
     *  记录用户搜索记录
     * @param type $user_id
     * @param type $keyword
     */
    public function addSearchRecord($user_id, $keyword) {

        $max_record_num = C('USER_SEARCH_RECORD_NUM');


        //没配置则不记录
        if (!is_numeric($max_record_num) || $max_record_num == 0)
            return true;

        $model = M('UserSearchRecord');
        $info = $model->find($user_id);


        //储存
        if (is_array($info)) {
            $keys = explode(',', $info['search_keyword']);

            //验证该关键词是否记录过
            $keyword_key = array_search($keyword, $keys);
            if (is_numeric($keyword_key))
                unset($keys[$keyword_key]);
            $keys[] = $keyword;

            //删减记录长度
            $new_keys = array_slice($keys, -$max_record_num);

            if (!is_array($new_keys))
                return false;


            $data = array (
                'update_time' => time(),
                'search_keyword' => implode(',', $new_keys)
            );


            return $model->where(array ('user_id' => $user_id))->save($data);
        }

        //初次记录

        $data = array (
            'update_time' => 0,
            'create_time' => time(),
            'search_keyword' => $keyword,
            'user_id' => $user_id,
        );

        return $model->add($data);
    }

    function getSearchRecord($user_id) {
        $model = M('UserSearchRecord');

        $info = $model->find($user_id);
        return !emptys($info['search_keyword']) ? explode(',', $info['search_keyword']) : array ();
    }

    /**
     *  获取用户积分
     * @param type $user_id
     * @return type
     */
    function getUserCredit($user_id) {
        $user_info = $this->field('credit')->find($user_id);

        return isset($user_info['credit']) ? (int)$user_info['credit'] : 0;
    }

    /**
     * 获取用户特征
     * @param type $user
     */
    function getUserFeatures($user_id) {

        $data = array ();
        if (!is_numeric($user_id))
            return $data;

        $where = array ('user_id' => $user_id);
        $data['area'] = array_cc(M('UserBusinessDistrict')->where($where)->select(), '', 'business_district_id');

        $data['shops'] = array_cc(M('UserLikeShop')->where($where)->select(), '', 'shop_id');

        $user_category = M('UserCategory')->where($where)->select();
        $category_list = getCategoryLabel();
        $data['category'] = array ();
        $data['kouwei'] = array ();
        foreach ($user_category as $c_info) {
            $c_id = $c_info['category_id'];
            foreach ($category_list[1]['label_list'] as $l_info) {
                if ($c_id == $l_info['id']) {
                    $data['category'][] = $c_id;
                    break;
                }
            }
            foreach ($category_list[2]['label_list'] as $l_info) {
                if ($c_id == $l_info['id']) {
                    $data['kouwei'][] = $c_id;
                    break;
                }
            }
        }
        return $data;
    }

    /**
     * @name 获取用户详情
     * @param $userid
     * @return array|mixed
     */
    public function getUserDetail($userid) {
        $data = array ();
        //1.获取用户基本信息
        $userInfo = $this->where("`id`={$userid}")->field('mobile,avatar,nickname,realname,credit')->find();
  
        if ($userInfo) {
            $data['user_info'] = $userInfo;
        } else {
            $data['user_info'] = new \stdClass();
        }
        //2.获取用户优惠券数量
        $data['coupon_num'] = M('coupon_list')->where("`user_id`={$userid}")->count();
        //3.获取用户赞过的店铺
        $data['giving_shop'] = M('user_giving_shop')->where("`user_id`={$userid}")->count();
 
        return $data;
    }

    /**
     * 更新用户积分
     * @param $user_id 用户id
     * @param $num 积分数
     */
    public function saveUserCredit($user_id, $num) {
        if ($num > 0) {
            $this->where("`id`={$user_id}")->setInc('credit', $num);
        }
        if ($num < 0) {
            $this->where("`id`={$user_id}")->setDec('credit', $num);
        }
    }

    /**
     * @name 保存昵称
     * @param $userid
     * @param $nickname
     */
    public function saveNickname($userid, $nickname) {
        return $this->where("`id`={$userid}")->save(array ('nickname' => $nickname));

    }

}
